package com.ruoyi.web.controller.sj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.framework.web.exception.base.XResult;
import com.ruoyi.sj.domain.Comments;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sj.domain.Dynamic;
import com.ruoyi.sj.service.IDynamicService;
import com.ruoyi.web.core.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;

/**
 * 朋友圈动态 信息操作处理
 *
 * @author tyc@yanyu.com
 * @date 2018-11-28
 */
@Controller
@RequestMapping("/sj/dynamic")
public class DynamicController extends BaseController {
    private String prefix = "sj/dynamic";

    @Autowired
    private IDynamicService dynamicService;

    @RequiresPermissions("sj:dynamic:view")
    @GetMapping()
    public String dynamic() {
        return prefix + "/dynamic";
    }

    /**
     * 查询朋友圈动态列表
     */
    @RequiresPermissions("sj:dynamic:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Dynamic dynamic) {
        startPage();
        List<Dynamic> list = dynamicService.selectDynamicList(dynamic);
        return getDataTable(list);
    }

    /**
     * 新增朋友圈动态
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存朋友圈动态
     */
    @RequiresPermissions("sj:dynamic:add")
    @Log(title = "朋友圈动态", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Dynamic dynamic) {
        return toAjax(dynamicService.insertDynamic(dynamic));
    }

    /**
     * 修改朋友圈动态
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Dynamic dynamic = dynamicService.selectDynamicById(id);
        mmap.put("dynamic", dynamic);
        return prefix + "/edit";
    }

    /**
     * 修改保存朋友圈动态
     */
    @RequiresPermissions("sj:dynamic:edit")
    @Log(title = "朋友圈动态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Dynamic dynamic) {
        return toAjax(dynamicService.updateDynamic(dynamic));
    }

    /**
     * 删除朋友圈动态
     */
    @RequiresPermissions("sj:dynamic:remove")
    @Log(title = "朋友圈动态", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dynamicService.deleteDynamicByIds(ids));
    }

    /**
     * 通过被评论用户id 和被评论动态id 查询 评论信息
     *
     * @return
     */
    @RequiresPermissions("sj:dynamic:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap map) {
        String[] sourceStrArray = id.split(",");
        map.put("id", sourceStrArray[0]);
        map.put("toMemberId", sourceStrArray[1]);
        return "sj/dynamic/comments";
    }


}
